/* 2010/12/8 */
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Cosmos.IO.FileSystemOperation;
using Cosmos.IO.FileSystemPath.Searcher;
using Cosmos.Windows.Themes;

namespace PictureManager
{
    public partial class OptionForm : Form
    {
        public OptionForm()
        {
            InitializeComponent();

            okButton.DialogResult = DialogResult.OK;
            cancelButton.DialogResult = DialogResult.Cancel;

            foreach (string aThemeName in ThemeManager.DisplayNames)
            {
                themeNameComboBox.Items.Add(aThemeName);
            }

            foreach (string aSearcherName in FilePathSearcherFactory.DisplayNames)
            {
                searcherNameComboBox.Items.Add(aSearcherName);
            }

            foreach (string anOperatorName in FileOperatorFactory.DisplayNames)
            {
                fileOperatorComboBox.Items.Add(anOperatorName);
            }

            playSpeedNumericUpDown.Maximum = 10M;
            playSpeedNumericUpDown.Minimum = 0.5M;
            playSpeedNumericUpDown.Increment = 0.5M;
            playSpeedNumericUpDown.DecimalPlaces = 1;
          
            LoadSettings();
        }

        private void LoadSettings()
        {
            Properties.Settings settings = Properties.Settings.Default;

            // File operation
            fileOperatorComboBox.SelectedIndex = 0;
            string operatorName = FileOperatorFactory.GetDisplayName(settings.FileOperatorName);
            if (operatorName != null)
            {
                foreach (object anItem in fileOperatorComboBox.Items)
                {
                    string itemText = (string)anItem;
                    if (itemText.Equals(operatorName))
                    {
                        fileOperatorComboBox.SelectedItem = anItem;
                        break;
                    }
                }
            }
            notifyFileSystemEventCheckBox.Checked = settings.NotifyFileSystemEvent;

            // Search.
            searcherNameComboBox.SelectedIndex = 0;
            string searcherName = FilePathSearcherFactory.GetDisplayName(settings.SearchName);
            if (searcherName != null)
            {
                foreach (object anItem in searcherNameComboBox.Items)
                {
                    string itemText = (string)anItem;
                    if (itemText.Equals(searcherName))
                    {
                        searcherNameComboBox.SelectedItem = anItem;
                        break;
                    }
                }
            }

            // Theme.
            themeNameComboBox.SelectedIndex = 0;
            string themeName = ThemeManager.GetDisplayName(settings.ThemeName);
            if (themeName != null)
            {
                foreach (object anItem in themeNameComboBox.Items)
                {
                    string itemText = (string)anItem;
                    if (itemText.Equals(themeName))
                    {
                        themeNameComboBox.SelectedItem = anItem;
                        break;
                    }
                }
            }
            playSpeedNumericUpDown.Value = settings.PlaySpeed;
            playDisorderCheckBox.Checked = settings.PlayDisorder;

            // SetPath.
            if ((settings.RenameSelectionKeeping & 0x0001) != 0x0)
                renameSelectionKeepCheckBox.Checked = true;
            else
            {
                renameSelectionKeepCheckBox.Checked = false;
                renameSelectionHeadRadioButton.Enabled = false;
                renameSelectionRearRadioButton.Enabled = false;
            }
            if ((settings.RenameSelectionKeeping & 0x0002) == 0x0)
                renameSelectionHeadRadioButton.Checked = true;
            else
                renameSelectionRearRadioButton.Checked = true;

            // Hide border.
            HideBorderSelection hideBorderSelection = new HideBorderSelection(settings.HideBorderSelection);
            hideBorderMainMenuCheckBox.Checked = hideBorderSelection.MainMenu;
            hideBorderMainStatusCheckBox.Checked = hideBorderSelection.MainStatus;
            hideBorderFolderTreeCheckBox.Checked = hideBorderSelection.FolderTree;
            hideBorderFileBasketCheckBox.Checked = hideBorderSelection.FileBasket;
            hideBorderFileToolCheckBox.Checked = hideBorderSelection.FileTool;
            hideBorderImageToolCheckBox.Checked = hideBorderSelection.ImageTool;
            hideBorderRenameToolCheckBox.Checked = hideBorderSelection.RenameTool;
            hideBorderSearchToolCheckBox.Checked = hideBorderSelection.SearchTool;
            hideBorderPageTabCheckBox.Checked = hideBorderSelection.PageTab;

            // Key combination.
            StringCollection keyCombinationExetants = settings.KeyCombinationExetants;
            if (keyCombinationExetants == null)
                keyCombinationExetants = new StringCollection();
            keyCombinationControl.LoadSettings(keyCombinationExetants);

            // Mouse gesture.
            StringCollection mouseGestureExecutants = settings.MouseGestureExecutants;
            if (mouseGestureExecutants == null)
                mouseGestureExecutants = new StringCollection();
            mouseGestureControl.LoadSettings(settings.MouseGestureColor, settings.MouseGestureWidth, mouseGestureExecutants);

            // File name attribute.
            StringCollection fileNameAttributes = settings.FileNameAttributes;
            if (fileNameAttributes == null)
                fileNameAttributes = new StringCollection();
            StringCollection fileNameColumnDefinitions = settings.FileNameColumnDefinitions;
            if (fileNameColumnDefinitions == null)
                fileNameColumnDefinitions = new StringCollection();
            fileNameAttributeControl.LoadSettings(fileNameAttributes, settings.FileNameAttributeIndex);
            
            // File name category.
            StringCollection fileCorrespondenceRules = settings.FileCorrespondenceRules;
            if (fileCorrespondenceRules == null)
                fileCorrespondenceRules = new StringCollection();
            fileNameCategoryControl.LoadSettings(settings.FileCorrespondedRootFolder, fileCorrespondenceRules);
        }

        private void SaveSettings()
        {
            Properties.Settings settings = Properties.Settings.Default;

            // File Operation
            string fileOperatorName = (string)fileOperatorComboBox.SelectedItem;
            settings.FileOperatorName = FileOperatorFactory.GetClassName(fileOperatorName);
            settings.NotifyFileSystemEvent = notifyFileSystemEventCheckBox.Checked;

            // Search.
            string searcherName = (string)searcherNameComboBox.SelectedItem;
            settings.SearchName = FilePathSearcherFactory.GetClassName(searcherName);

            // Theme.
            string themeName = (string)themeNameComboBox.SelectedItem;
            settings.ThemeName = ThemeManager.GetClassName(themeName);
            settings.PlaySpeed = playSpeedNumericUpDown.Value;
            settings.PlayDisorder = playDisorderCheckBox.Checked;

            // SetPath.
            if (renameSelectionKeepCheckBox.Checked)
                settings.RenameSelectionKeeping |= 0x0001;
            else
                settings.RenameSelectionKeeping &= 0xFFFE;

            if (renameSelectionHeadRadioButton.Checked)
                settings.RenameSelectionKeeping &= 0xFFFD;
            else if (renameSelectionRearRadioButton.Checked)
                settings.RenameSelectionKeeping |= 0x0002;

            // Hide border.
            HideBorderSelection hideBorderSelection = new HideBorderSelection(
                hideBorderMainMenuCheckBox.Checked, hideBorderMainStatusCheckBox.Checked
                , hideBorderFolderTreeCheckBox.Checked, hideBorderFileBasketCheckBox.Checked
                , hideBorderFileToolCheckBox.Checked, hideBorderImageToolCheckBox.Checked
                , hideBorderRenameToolCheckBox.Checked, hideBorderSearchToolCheckBox.Checked
                , hideBorderPageTabCheckBox.Checked);
            settings.HideBorderSelection = hideBorderSelection.EscapeNumber;

            // Key combination.
            StringCollection keyCombinationExetants = settings.KeyCombinationExetants;
            if (keyCombinationExetants == null)
                keyCombinationExetants = new StringCollection();
            keyCombinationControl.SaveSettings(keyCombinationExetants);
            settings.KeyCombinationExetants = keyCombinationExetants;

            // Mouse gesture.
            Color mouseGestureColor;
            float mouseGestureWidth;
            StringCollection mouseGestureExecutants = settings.MouseGestureExecutants;
            if (mouseGestureExecutants == null)
                mouseGestureExecutants = new StringCollection();
            mouseGestureControl.SaveSettings(out mouseGestureColor, out mouseGestureWidth, mouseGestureExecutants);
            settings.MouseGestureColor = mouseGestureColor;
            settings.MouseGestureWidth = mouseGestureWidth;
            settings.MouseGestureExecutants = mouseGestureExecutants;

            // File name attribute.
            int fileNameAttributeIndex;
            StringCollection fileNameAttributes = settings.FileNameAttributes;
            if (fileNameAttributes == null)
                fileNameAttributes = new StringCollection();
            StringCollection fileNameColumnDefinitions = settings.FileNameColumnDefinitions;
            if (fileNameColumnDefinitions == null)
                fileNameColumnDefinitions = new StringCollection();
            fileNameAttributeControl.SaveSettings(fileNameAttributes, out fileNameAttributeIndex);
            settings.FileNameAttributeIndex = fileNameAttributeIndex;
            settings.FileNameAttributes = fileNameAttributes;
            settings.FileNameColumnDefinitions = fileNameColumnDefinitions;

            // File name category.
            string fileCorrespondedRootFolder; 
            StringCollection fileCorrespondenceRules = settings.FileCorrespondenceRules;
            if (fileCorrespondenceRules == null)
                fileCorrespondenceRules = new StringCollection();
            fileNameCategoryControl.SaveSettings(out fileCorrespondedRootFolder, fileCorrespondenceRules);
            settings.FileCorrespondedRootFolder = fileCorrespondedRootFolder;
            settings.FileCorrespondenceRules = fileCorrespondenceRules;

            settings.Save();
        }

        private void OptionForm_Load(object sender, EventArgs e)
        {
            Properties.OptionFormSettings settings = Properties.OptionFormSettings.Default;

            Size = settings.FormSize;
            Location = settings.FormLocation;
        }

        private void OptionForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            Properties.OptionFormSettings settings = Properties.OptionFormSettings.Default;

            settings.FormSize = Size;
            settings.FormLocation = Location;

            settings.Save();
        }

        private void okButton_Click(object sender, EventArgs e)
        {
            SaveSettings();
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {

        }

        private void applyButton_Click(object sender, EventArgs e)
        {
            SaveSettings();
        }

        private void renameSelectionKeepCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox self = sender as CheckBox;
            renameSelectionHeadRadioButton.Enabled = self.Checked;
            renameSelectionRearRadioButton.Enabled = self.Checked;
        }

        public bool BasicTabPageSelected
        {
            get { return tabControl.SelectedTab.Equals(basicOptionTabPage); }
            set { if (value)tabControl.SelectedTab = basicOptionTabPage; }
        }

        public bool KeyCombinationTabPageSelected
        {
            get { return tabControl.SelectedTab.Equals(keyCombinationTabPage); }
            set { if (value)tabControl.SelectedTab = keyCombinationTabPage; }
        }

        public bool MouseGestureTabPageSelected
        {
            get { return tabControl.SelectedTab.Equals(mouseGestureTabPage); }
            set { if (value)tabControl.SelectedTab = mouseGestureTabPage; }
        }

        public bool FileNameAttributeTabPageSelected
        {
            get { return tabControl.SelectedTab.Equals(fileNameAttributeTabPage); }
            set { if (value)tabControl.SelectedTab = fileNameAttributeTabPage; }
        }

        public bool FileNameCategoryTabPageSelected
        {
            get { return tabControl.SelectedTab.Equals(fileNameCategoryTabPage); }
            set { if (value)tabControl.SelectedTab = fileNameCategoryTabPage; }
        }
    }
}